//
//  CSBSevcondSweeping.swift
//  CurSodoboAer
//
//  Created by CurSodoboAer on 2024/11/5.
//

import UIKit

class CSBSevcondSweeping: CSBComeenrSweeping,UITableViewDelegate,UITableViewDataSource {
    private var cakeCSB:Dictionary<String,CGPoint>?
    private var fookCSB: UITextField?
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return CSBFetcherAll.Hammer.allMyChatListCSB.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cominData = CSBFetcherAll.Hammer.allMyChatListCSB[indexPath.row]
        
        let normalDAta = tableView.dequeueReusableCell(withIdentifier: "CSBlistChatCellID", for: indexPath) as! CSBMesdListfCell
       
        normalDAta.selectionStyle = .none
        
        if let dkjg = cominData.chatCSBList.first{
            normalDAta.mychatContetnCSBLab.text = dkjg.1
        }else{
            normalDAta.mychatContetnCSBLab.text = ""
        }
        
        normalDAta.headCCSBL.image = UIImage(named: cominData.whoToMeCSBDic["csbpersonHead"] ?? "")
        normalDAta.userNameCSBLab.text = cominData.whoToMeCSBDic["csbpersonName"] ?? ""
        return normalDAta
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let someg = CSBFetcherAll.Hammer.allMyChatListCSB[indexPath.row]
        
        self.navigationController?.pushViewController(CSBMesgEnterSweeping.init(qugaoshCSB:[334,445],mesgDataCSB: someg), animated: true)
        
    }
    private let toplazuCover = UIView.init()
    var userListONlineVirew = [CSBMesgAvoter]()
    let onlinetikh = UILabel.init()
    private let comeAICSBACti = UIButton.init()
    
    lazy var mesglistViewCSB: UITableView = {



        let allmsdf = UITableView.init()
        allmsdf.backgroundColor = .clear
        
        allmsdf.contentInset = UIEdgeInsets.zero
        
        allmsdf.rowHeight = 114
        allmsdf.register(CSBMesdListfCell.self, forCellReuseIdentifier: "CSBlistChatCellID")
        allmsdf.separatorStyle = .singleLine
//        allmsdf.allowsSelection = false
        allmsdf.showsVerticalScrollIndicator = false
    
        allmsdf.dataSource = self
        allmsdf.delegate = self
    
        return allmsdf
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        mesglistViewCSB.reloadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        toplazuCover.backgroundColor = UIColor(red: 0.75, green: 0.98, blue: 0.25, alpha: 1)
        
        view.backgroundColor = UIColor(red: 0.64, green: 0.41, blue: 0.98, alpha: 1)
        comeAICSBACti.setBackgroundImage(UIImage.init(named: "csb_AI"), for: .normal)
        comeAICSBACti.addTarget(self, action: #selector(normalPickAISegmengt), for: .touchUpInside)
        NotificationCenter.default.addObserver(self, selector: #selector(setupNewONlines), name: NSNotification.Name.init("laheiCSBkUsers"), object: nil)
        view.addSubview(toplazuCover)
        view.addSubview(comeAICSBACti)
        toplazuCover.snp.makeConstraints { make in
            make.leading.trailing.top.equalToSuperview()
            make.height.equalTo(268.xQQ + 26.xQQ + 6)
        }
        let stattusBarHeight = UIApplication.shared.statusBarFrame.height
        comeAICSBACti.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(stattusBarHeight + 8)
            make.right.equalToSuperview().inset(16.xQQ)
            make.width.height.equalTo(44.xQQ)
        }
        
        
        let meadtikh = UILabel.init()
        meadtikh.text = "Message"
        meadtikh.textColor = UIColor(red: 0.07, green: 0.07, blue: 0.07, alpha: 1)
        meadtikh.font = UIFont(name: "Futura Bold", size: 30)
        view.addSubview(meadtikh)
        meadtikh.snp.makeConstraints { make in
            make.centerY.equalTo(comeAICSBACti)
            make.leading.equalToSuperview().inset(16.xQQ)
        }
        
        
        onlinetikh.text = "Online Coach"
        onlinetikh.textColor =  UIColor(red: 0.07, green: 0.07, blue: 0.07, alpha: 1)
        onlinetikh.font = UIFont(name: "Futura Medium", size: 18)
        view.addSubview(onlinetikh)
        onlinetikh.snp.makeConstraints { make in
            make.top.equalTo(meadtikh.snp.bottom).offset(20.xQQ)
            make.leading.equalToSuperview().inset(16.xQQ)
        }
        
        
        setupNewONlines()
        
        let recentikh = UILabel.init()
        recentikh.text = "Recent"
        recentikh.textColor = UIColor.white
        recentikh.font = UIFont(name: "Basteleur Bold", size: 18)
        view.addSubview(recentikh)
        recentikh.snp.makeConstraints { make in
            make.top.equalTo(toplazuCover.snp.bottom).offset(32.xQQ)
            make.leading.equalToSuperview().inset(16.xQQ)
        }
        if fookCSB == nil{
            fookCSB = UITextField.init()
            fookCSB?.backgroundColor = .clear
            fookCSB?.leftViewMode = .never
            fookCSB?.placeholder = nil
        }
        
        
        if cakeCSB == nil{
            cakeCSB = Dictionary.init()
        }
        
        var poinrtCSB = CGPoint.init(x: 23, y: 34)
        
        cakeCSB?["\(CGFloat.random(in: 0...5))"] = poinrtCSB
        
        if fookCSB?.text == nil {
            cakeCSB?["\(CGFloat.random(in: 5...20))"] = poinrtCSB
        }
        poinrtCSB.y += 10
        
        if cakeCSB?.keys.count ?? 0 > 0 {
            
        }
        
        if poinrtCSB.y > 2 && poinrtCSB.x > 2 {
            view.addSubview(mesglistViewCSB)
            mesglistViewCSB.snp.makeConstraints { make in
                make.leading.trailing.bottom.equalToSuperview()
                make.top.equalTo(recentikh.snp.bottom)
            }
        }
        
    }
    @objc func normalPickAISegmengt() {
        self.navigationController?.pushViewController(CSBAISweeping.init(), animated: true)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        toplazuCover.layer.cornerRadius = 30
        toplazuCover.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
    }
    
    @objc func setupNewONlines()  {
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

       
        for item in self.userListONlineVirew {
            item.removeFromSuperview()
        }
        for (ij,Iter) in CSBFetcherAll.Hammer.folooweNewstCSB.enumerated() {
            let avaor = CSBMesgAvoter.init()
            csbBayui.append(contentsOf: [52, 6, 11])
            csbBayui.removeLast()
            cabSunmAll = csbBayui.count

            if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
                avaor.addTarget(self, action: #selector(bequickLao(jnk:)), for: .touchUpInside)
                avaor.userINfoCSB = Iter
            }
            
            self.view.addSubview(avaor)
            avaor.snp.makeConstraints { make in
                make.width.equalTo(70.xQQ)
                make.height.equalTo(90 + 28.xQQ + 6)
                make.leading.equalToSuperview().offset(12.xQQ + 88.xQQ*CGFloat(ij))
                make.top.equalTo(onlinetikh.snp.bottom).offset(16.xQQ)
            }
            userListONlineVirew.append(avaor)
        }
        
        self.mesglistViewCSB.reloadData()
    }

    
    @objc func bequickLao(jnk:CSBMesgAvoter) {
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        csbBayui.append(contentsOf: [52, 6, 11])
        csbBayui.removeLast()
        cabSunmAll = csbBayui.count

       
        if let userinfo = jnk.userINfoCSB {
            if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
                self.navigationController?.pushViewController( CSBuservcerSweeping.init(qugaoshCSB:[334,445], alluserDataCSB:userinfo ), animated: true)
            }
           
        }
        
    }
}


class CSBMesgAvoter: UIButton {
    private var dymOImagerCSB = UIImageView.init()
    private var bauNameCSB = UILabel.init()
    var userINfoCSB:Dictionary<String,String>? {
        didSet{
            dymOImagerCSB.image = UIImage(named: userINfoCSB?["csbpersonHead"] ?? "")
            bauNameCSB.text = userINfoCSB?["csbpersonName"] ?? ""
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        let dbottomagerCSB = UIImageView.init(image: UIImage.init(named: "Csbvifeo"))
        dbottomagerCSB.contentMode = .scaleAspectFill
        
        
        dymOImagerCSB.contentMode = .scaleAspectFill
        dymOImagerCSB.layer.masksToBounds = true
        dymOImagerCSB.layer.cornerRadius = 35.xQQ
        bauNameCSB.textColor = UIColor(red: 0.07, green: 0.07, blue: 0.07, alpha: 1)
        bauNameCSB.font =   UIFont(name: "Helvetica", size: 14)
        bauNameCSB.textAlignment = .center
        
        addSubview(dymOImagerCSB)
        dymOImagerCSB.snp.makeConstraints { make in
            make.width.height.equalTo(70.xQQ)
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        addSubview(bauNameCSB)
        bauNameCSB.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(dymOImagerCSB.snp.bottom).offset(6.xQQ)
        }
        
        addSubview(dbottomagerCSB)
        dbottomagerCSB.snp_makeConstraints { make in
            make.width.equalTo(52.xQQ)
            make.height.equalTo(28.xQQ)
            make.centerX.equalToSuperview()
            make.top.equalTo(bauNameCSB.snp.bottom).offset(8)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
